class TDCMWButtonTagLib {

    static namespace = 'tdcmw'

    // Buttons
    def backButton = {attrs ->
        out << '<button class="button" onClick=\"history.back()\"'

        if (attrs.disabled) {
            out << ' disabled="true"'
        }

        out << ">Back</button>"
    }

    def linkButton = {attrs ->
        if (!attrs.controller) {
            attrs.controller = controllerName
        }
        if (attrs.params?.id) {
            attrs.id = attrs.params.id
            attrs.params.id = ""
        }

        def location = "parent.location='${createLink(controller:attrs.controller, action:attrs.action, id:attrs.id, params:attrs.params)}'"

        out << '<button class="button" onClick="'
        if (attrs.confirm) {
            out << "if (confirm('$attrs.confirm')) {$location}\""
        } else {
            out << "$location\""
        }

        if (attrs.disabled) {
            out << ' disabled="true"'
        }

        out << ">$attrs.label</button>"
    }

    def submitFormButton = {attrs ->
        out << '<button class="button" type="submit" '
        if (attrs.confirm) {
            out << "onClick=\"return confirm('$attrs.confirm')\" "
        }
        out << "name=_action_$attrs.action"

        if (attrs.disabled) {
            out << ' disabled="true"'
        }

        out << ">$attrs.label</button>"
    }

    def showDialogButton = {attrs ->
        out << "<button type='button' class='button' id=$attrs.id "

        if (attrs.disabled) {
            out << ' disabled="true"'
        }

        out << ">$attrs.label</button>"
    }
}
